﻿using BigEgg.Framework.Foundation;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace FMStudio.Domain.SQLite
{
    [MetadataType(typeof(ICurrencyExchange))]
    public partial class CurrencyExchange : ICurrencyExchange, IDataErrorInfo
    {
        #region Implement IDataErrorInfo Interface
        [NonSerialized]
        private readonly DataErrorInfoSupport dataErrorInfoSupport;

        string IDataErrorInfo.Error { get { return this.dataErrorInfoSupport.Error; } }

        string IDataErrorInfo.this[string memberName] { get { return this.dataErrorInfoSupport[memberName]; } }
        #endregion


        public CurrencyExchange()
        {
            this.dataErrorInfoSupport = new DataErrorInfoSupport(this);

            //  Default value
            this.Date = DateTime.Today;
            this.Price = 100.0;

            this.FromReference.AssociationChanged += FromReferenceAssociationChanged;
            this.ToReference.AssociationChanged += ToReferenceAssociationChanged;
        }


        #region Implement ICurrencyExchange Interface
        ICurrency ICurrencyExchange.From
        {
            get { return this.From; }
            set { this.From = value as Currency; }
        }

        ICurrency ICurrencyExchange.To
        {
            get { return this.To; }
            set { this.To = value as Currency; }
        }
        #endregion


        #region Private Methods
        private void ToReferenceAssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            // The navigation property doesn't support the PropertyChanged event. We have to raise it ourselves.
            OnPropertyChanged("To");
            this.ToID = this.To.ID;
        }

        private void FromReferenceAssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            // The navigation property doesn't support the PropertyChanged event. We have to raise it ourselves.
            OnPropertyChanged("From");
            this.FromID = this.From.ID;
        }
        #endregion
    }
}
